<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/26/0026
 * Time: 11:40
 */

namespace app\sports_admin\service;
use app\sports_admin\model\SportProject as SportProjectModel;
use app\exception\SportProjectException;
use think\Request;

class SportProject
{
    /**
     * 更新类别信息
     */
    public static function update()
    {
        $info = self::getParamsInfo();
        $bool = (new SportProjectModel())->save(['title'=>$info['title']],['id'=>$info['id']]);
        if(!$bool){
            throw new SportProjectException([
                'msg'=>'修改失败',
                'errorCode'=>400
            ]);
        }
        return json(['code'=>201,'msg'=>'修改成功']);
    }

    /**
     * 获取参数
     */
    private static function getParamsInfo()
    {
        return Request::instance()->param();
    }
    /**
     * 添加运动项目名
     */
    public static function sportProjectAdd()
    {
        $data = Request::instance()->only('name');
        //检测是否重名
        $is_repeat =  SportProjectModel::sportProjectRepeat($data['name']);
        if(!empty($is_repeat)){
            throw new SportProjectException(['errorCode'=>400,'msg'=>'已经存在的分类名']);
        }
        return SportProjectModel::sportProjectAdd($data['name']);
    }
}